package com.kuang.controller;

import com.kuang.dao.DepartmentDao;
import com.kuang.dao.EmployDao;
import com.kuang.pojo.Department;
import com.kuang.pojo.Employee;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Collection;

@Controller
public class EmployeeController {

    @Autowired
    EmployDao employDao;
    @Autowired
    DepartmentDao departmentDao;

    @RequestMapping("/emps")
    public String list(Model model){
        Collection<Employee> employees = employDao.getAll();
        model.addAttribute("emps",employees);
        return "emp/list";
    }

    @GetMapping("/emp")
    public String toAddpage(Model model){
        //跳转页面的时候查出所有部门的信息
        Collection<Department> departments = departmentDao.getDepartments();
        model.addAttribute("departments",departments);
        return "emp/add";
    }

    @PostMapping("/emp")
    public String addEmp(Employee employee){
        //添加的操作
//        System.out.println("save==>"+employee);
//        employDao.save(employee); //调用底层业务方法保存信息
        return "redirect:/emps";
    }


    //去员工的修改页面
    public String toUpdateEmp(@PathVariable("id") Integer id,Model model){
        Employee employee = employDao.getEmployeeById(id);

        model.addAttribute("emp",employee);
        return "emp/update";
    }
}
